﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DateTimeCalendar : System.Web.UI.UserControl
{

  private DateTime _selectedDateTime;

  private DateTime ? _maximumDateTime;

  private DateTime ? _minimumDateTime;


  /// <summary>
  /// Returns a string that display's the DateTime in a summary state
  /// </summary>
  /// <param name="dt"></param>
  /// <param name="showTime"></param>
  /// <returns></returns>
  public static string Format(DateTime ? dt, bool showTime)
  {
    string str = "";

    //if (showTime)
    //  str = String.Format(@"{0:MM-d-yyyy h:mm }", dt) + String.Format("{0:tt}", dt).ToLower();
    //else
    //  str = String.Format(@"{0:MM-d-yyyy}", dt);
    if (showTime)
        str = String.Format(@"{0:d MMM yyyy h:mm }", dt) + String.Format("{0:tt}", dt).ToLower();
    else
        str = String.Format(@"{0:d MMM yyyy}", dt);

    return str;
  }


  /// <summary>
  //the maximum allowable DateTime
  /// </summary>
  public DateTime ? MaximumDateTime 
  {
    get
    {
      string str;

      str = this.Attributes["MaximumDateTime"];

      if (!string.IsNullOrEmpty(str))
        _maximumDateTime = DateTime.Parse(str);

      return _maximumDateTime;
    }
    set
    {
      _maximumDateTime = value;
    }
  }


  /// <summary>
  //the minimum allowable DateTime
  /// </summary>
  public DateTime ? MinimumDateTime
  {
    get
    {
      string str;

      str = this.Attributes["MinimumDateTime"];

      if (!string.IsNullOrEmpty(str))
        _minimumDateTime = DateTime.Parse(str);

      return _minimumDateTime;
    }
    set
    {
      _minimumDateTime = value;
    }
  }


  /// <summary>
  ///the DateTime to display
  /// </summary>
  public DateTime SelectedDateTime
  {
    get
    {
      if (IsPostBack)
      {
        string str = Page.Request.Form[ID];
        _selectedDateTime = DateTime.Parse(str);
      }
      else
      {
        string str;

        str = this.Attributes["SelectedDateTime"];

        if (!string.IsNullOrEmpty(str))
          _selectedDateTime = DateTime.Parse(str);
        else if (_selectedDateTime.Year == 1)
          _selectedDateTime = DateTime.Now;
      }

      return _selectedDateTime;
    }

    set
    {
      _selectedDateTime = value;
    }
  }


  /// <summary>
  /// false to not show the time, the default is true
  /// </summary>
  public bool ShowTime 
  {
    get
    {
      bool showTime = true;
      string str;

      str = this.Attributes["ShowTime"];

      if (!string.IsNullOrEmpty(str))
      {
        showTime = Convert.ToBoolean(str);
      }

      return showTime;
    }
  }


  protected void Page_Load(object sender, EventArgs e)
  {
  }


  protected override void Render(HtmlTextWriter writer)
  {

    if (ID.Length == 0)
      writer.Write("You must enter set the Id of the DateTimeCalendar!");

    writer.Write("\n<input name=\"" + ID + "\" id =\"" + ID + "\" type=\"text\" class=\"TimeCalendar\" value=\"" + Format(SelectedDateTime, ShowTime) + "\" />");

    writer.Write("\n\n<script type=\"text/javascript\">");

    writer.Write("\n\n$(document).ready(function() {");

    writer.Write("\n\n  var dt_" + ID + " = $(\"#" + ID + "\");");

    if(MinimumDateTime != null)
      writer.Write("\n  dt_" + ID + ".attr(\"" + "minimumDate\", \"" + Format(MinimumDateTime, ShowTime) + "\");");

    if (MaximumDateTime != null)
      writer.Write("\n  dt_" + ID + ".attr(\"" + "maximumDate\", \"" + Format(MaximumDateTime, ShowTime) + "\");");

    writer.Write("\n  dt_" + ID + ".attr(\"" + "showTime\", \"" + ShowTime.ToString().ToLower() + "\");");

    writer.Write("\n\n});");

    writer.Write("\n\n</script>\n\n");

  }

}
